package com.collect.cameroon.zhiScene.zhiWidget.zhiOptionView;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.collect.cameroon.R;

import java.util.ArrayList;

public class ZHIOptionView extends PopupWindow {

    private View mMenuView;
    private LinearLayout ll_pop_layout;
    private RecyclerView rc_list;
    private String mType;

    private ArrayList<ZHIOption> mOptions;
    ZHIOptionAdjuster mZHIOptionAdjuster = new ZHIOptionAdjuster();

    private OnOptionClickListener mOnOptionClickListener;

    public ZHIOptionView(Context context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popview_option, null);
        ll_pop_layout = mMenuView.findViewById(R.id.ll_pop_layout);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopWindowFromBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x30000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setClippingEnabled(false);
        rc_list = mMenuView.findViewById(R.id.rc_list);

        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.ll_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        rc_list.setLayoutManager(new GridLayoutManager(context, 1));
        rc_list.setAdapter(mZHIOptionAdjuster);
        mZHIOptionAdjuster.setOptionClickListener(new ZHIOptionAdjuster.OptionClickListener() {
            @Override
            public void OnItemClick(String title) {
                if (mOnOptionClickListener != null) {
                    mOnOptionClickListener.onOptionClick(mType, title);
                }
                dismiss();
            }
        });
    }

    public void setOptionTitle(String title) {
        TextView tv_option_title = (TextView) mMenuView.findViewById(R.id.tv_option_title);
        tv_option_title.setText(title);
    }

    public void setNewData(ArrayList<ZHIOption> options) {
        mOptions = options;
        mZHIOptionAdjuster.setNewData(mOptions);
    }

    public void setOptionType(String type) {
        this.mType = type;
    }

    public void setOptionClickListener(OnOptionClickListener optionClickListener) {
        this.mOnOptionClickListener = optionClickListener;
    }

    public interface OnOptionClickListener {
        void onOptionClick(String type, String result);
    }

}